package com.zh.manager;

import java.util.concurrent.*;

public class ThreadManager {
    private static final ExecutorService fixedThreadPool;

    static {
        ThreadFactory threadFactory = r -> {
            Thread thread = new Thread(r);
            thread.setName("thread-common-" + thread.getId());
            return thread;
        };

        fixedThreadPool = new ThreadPoolExecutor(
                2, // 核心线程数
                10, // 最大线程数
                30L, // 空闲线程存活时间
                TimeUnit.SECONDS, // 时间单位
                new ArrayBlockingQueue<>(50), // 任务队列
                threadFactory, // 线程工厂
                new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略
        );
    }

    public static void execute(Runnable task) {
        fixedThreadPool.execute(task);
    }

    public static void shutdown() {
        fixedThreadPool.shutdown();
    }

    public static void shutdownNow() {
        fixedThreadPool.shutdownNow();
    }

    public static boolean isShutdown() {
        return fixedThreadPool.isShutdown();
    }

    public static boolean isTerminated() {
        return fixedThreadPool.isTerminated();
    }

    public static boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
        return fixedThreadPool.awaitTermination(timeout, unit);
    }
}
